<script setup>
import { ref } from 'vue'
import { useUserInfoStore } from '@/stores/userInfo';
import { useTokenStore } from '@/stores/token';
const tokenStore = useTokenStore()
const userInfoStore = useUserInfoStore()
const userInfo = ref({ ...userInfoStore.info })


const resetPwdData = ref({
    oldPwd: '',
    newPwd: '',
    rePwd: ''
})

//自定义确认密码的校验函数
const rePasswordValid = (rule, value, callback) => {
    if (value == null || value === '') {
        return callback(new Error('请再次确认密码'))
    }
    if (resetPwdData.value.newPwd !== value) {
        return callback(new Error('两次输入密码不一致'))
    }
}
//用于注册的表单校验模型
const rules = {
    oldPwd: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 5, max: 16, message: '密码长度必须为5~16位', trigger: 'blur' }
    ],
    newPwd: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 5, max: 16, message: '密码长度必须为5~16位', trigger: 'blur' }
    ],
    rePwd: [
        { validator: rePasswordValid, trigger: 'blur' }
    ]
}

import { userPwdUpdateService } from '@/api/user';
import { ElMessage } from 'element-plus';

// const updatePwd = async()=>{
    
//     let result = await userPwdUpdateService(resetPwdData.value)
//     console.log(resetPwdData.value)
//     ElMessage.success(result.message? result.message:'修改成功')

//     //清除token
//     tokenStore.removeToken()

// }

import { useRouter } from 'vue-router';
const router = useRouter()

const updatePwd = async () => {  
    try {  
        let result = await userPwdUpdateService(resetPwdData.value,tokenStore.token);  
        console.log(resetPwdData.value);  
        ElMessage.success(result.message ? result.message : '修改成功');  

        // 清除 token  
        tokenStore.removeToken(); 
        router.push('/login') 
    } catch (error) {  
        console.error("密码更新失败:", error);  
        ElMessage.error("密码更新失败，请稍后再试");  
    }  
};  


</script>
<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <span>修改密码</span>
            </div>
        </template>
        <el-row>
            <el-col :span="12">
                <el-form :model="resetPwdData" :rules="rules" label-width="100px" size="large">
                    <el-form-item label="旧密码" prop="oldPwd">
                        <el-input :prefix-icon="Lock" type="password" placeholder="请输入旧密码" v-model="resetPwdData.oldPwd" show-password></el-input>
                    </el-form-item>
                    <el-form-item label="新密码" prop="newPwd">
                        <el-input :prefix-icon="Lock" type="password" placeholder="请输入新密码" v-model="resetPwdData.newPwd" show-password  ></el-input>
                    </el-form-item>
                    <el-form-item label="确认密码" prop="rePwd">
                        <el-input :prefix-icon="Lock" type="password" placeholder="请确认新密码" v-model="resetPwdData.rePwd" show-password></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="updatePwd">提交修改</el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
    </el-card>
</template>